package com.ruoyi.xuexiao.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.xuexiao.domain.entity.KsSsjCqjl;
import com.ruoyi.xuexiao.domain.entity.KsTc;
import com.ruoyi.xuexiao.domain.param.*;
import com.ruoyi.xuexiao.domain.vo.*;
import com.ruoyi.xuexiao.service.KsJbxxhzxService;
import com.ruoyi.xuexiao.service.KsSsjCqjgService;
import com.ruoyi.xuexiao.service.KsSsjCqjlService;
import com.ruoyi.xuexiao.service.KsTcsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.net.URLEncoder;
import java.util.List;


/**
 * 矿山基本信息汇总表控制层
 *
 * @author: 齐浩宽
 * @date: 2024年5月27日16:32:47
 */
@Api(tags = "双随机-矿山基本信息汇总")
@RestController
@AllArgsConstructor
public class KsJbxxhzsController extends BaseController {
    /**
     * 矿山基本信息汇总表Service层
     */

    private KsJbxxhzxService ksJbxxhzxService;
    /**
     * 矿山底数抽查记录抽取结果Service层
     */
    private KsSsjCqjgService ksSsjCqjgService;
    /**
     * 矿山底数抽查抽取记录Service层
     */
    private KsSsjCqjlService ksSsjCqjlService;
    /**
     * 矿山图层Service层
     */
    private KsTcsService ksTcService;


    /**
     * 查询矿山基本信息汇总表 地区 统计
     *
     * @author 齐浩宽
     * @since 2024年5月27日17:06:12
     */
    @GetMapping("/xhz/queryStatistics")
    @ApiOperation(value = "查询矿山基本信息汇总表地区统计", notes = "查询矿山基本信息汇总表地区统计")
    public R<KsJbVo> queryStatistics() {
        return R.ok(ksJbxxhzxService.queryStatistics());
    }


    /**
     * 查询矿山基本信息汇总表 抽查数据
     *
     * @author 齐浩宽
     * @since 2024年5月27日17:06:12
     */
    @PostMapping("/xhz/getKsJbxhz")
    @ApiOperation(value = "查询矿山基本信息汇总表抽查数据", notes = "查询矿山基本信息汇总表抽查数据")
    public R<List<KsJbxxhzVo>> getKsJbxhz(@Validated @RequestBody KsJbxxhzParam ksJbxxhzParam) {
        return R.ok(ksJbxxhzxService.getKsJbxhz(ksJbxxhzParam));
    }

    /**
     * 导出抽取数据(未抽取)
     *
     * @author 齐浩宽
     * @since 2024年5月27日17:06:42
     */
    @ApiOperation(value = "导出抽取数据(未抽取)", notes = "导出抽取数据(未抽取)")
    @PostMapping("/xhz/exportExtractData")
    public void exportExtractData(HttpServletResponse response, @RequestBody List<KsJbxxhzVo> jbxxhzVoList) throws UnsupportedEncodingException {
        //设置请求体
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("矿权抽取数据.xlsx", "UTF-8"));
        ExcelUtil<KsJbxxhzVo> util = new ExcelUtil<>(KsJbxxhzVo.class);
        util.exportExcel(response, jbxxhzVoList, "矿权抽取数据");
    }

    /**
     * 查询抽取记录
     *
     * @author 齐浩宽
     * @since 2024年5月27日17:06:54
     */
    @GetMapping("/xhz/queryRecords")
    @ApiOperation(value = "查询抽取记录", notes = "查询抽取记录")
    public TableDataInfo<KsSsjCqjl> queryRecords(@RequestParam(value = "year", required = false) String year) {
        startPage();
        List<KsSsjCqjl> ksSsjCqjls = ksSsjCqjlService.queryRecords(year);
        return getDataTable(ksSsjCqjls);
    }

    /**
     * 修改抽取记录状态为完成
     *
     * @author 齐浩宽
     * @since 2024年5月27日17:07:10
     */
    @GetMapping("/xhz/updateCqwc")
    @ApiOperation("修改抽取记录状态为完成")
    public R<Boolean> updateCqwc(@RequestParam(value = "id", required = false) Integer id, @RequestParam(value = "year", required = false) String year) {
        boolean updateCqwc = ksSsjCqjlService.updateCqwc(id, year);
        return updateCqwc ? R.ok() : R.fail();
    }

    /**
     * 查询抽取记录详情
     *
     * @author 齐浩宽
     * @since 2024年5月27日17:07:20
     */
    @GetMapping("/xhz/queryTheDetailsOfTheRecord")
    @ApiOperation("查询抽取记录详情")
    public TableDataInfo<KsSsjCqjgVo> queryTheDetailsOfTheRecord(@Validated KsSsjCqjgParam ksSsjCqjg) {
        return ksSsjCqjgService.queryTheDetailsOfTheRecord(ksSsjCqjg);
    }

    /**
     * 导出抽取记录(抽取)
     *
     * @author 齐浩宽
     * @since 2024年5月27日17:07:30
     */
    @ApiOperation("导出抽取记录(抽取)")
    @PostMapping("/xhz/exportExtractRecordDetails")
    public void exportExtractRecordDetails(HttpServletResponse response, @RequestBody KsSsjCqjgParam ksSsjCqjg) throws UnsupportedEncodingException {
        List<KsSsjCqjgExportVo> ksSsjCqjgs = ksSsjCqjgService.exportExtractRecordDetails(ksSsjCqjg);
        //设置请求体
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("双随机一公开列表.xlsx", "UTF-8"));
        ExcelUtil<KsSsjCqjgExportVo> util = new ExcelUtil<KsSsjCqjgExportVo>(KsSsjCqjgExportVo.class);
        util.exportExcel(response, ksSsjCqjgs, "双随机一公开列表");
    }


    /**
     * 分页查询矿山图层管理数据
     *
     * @author 窦凯欣
     */
    @ApiOperation(value = "分页查询矿山图层管理数据")
    @GetMapping("/mineLayersController/getAll")
    public TableDataInfo<KsJbxxhzLayerManagementVo> dataAll(MineLayerDataParam mineLayerDataParam) {
        return ksJbxxhzxService.dataAll(mineLayerDataParam);
    }

    /**
     * 上传zip解压shp文件信息存入数据库
     *
     * @author 窦凯欣
     */
    @ApiOperation(value = "上传zip解压shp文件信息存入数据库")
    @ApiImplicitParam(name = "file", value = "上传文件", type = "MultipartFile")
    @PostMapping("/mineLayersController/uploadShp")
    public R uploadShp(@NotNull(message = "没有上传相关文件") @RequestBody MultipartFile file) throws IOException {
        return R.ok(ksTcService.uploadShp(file));
    }

    /**
     * 根据id删除shp文件信息
     *
     * @author 窦凯欣
     */
    @ApiOperation(value = "根据id删除shp文件信息")
    @ApiImplicitParam(name = "id", value = "列表数据id", type = "Long")
    @PostMapping("/mineLayersController/delete")
    public R deleteByid(@RequestBody Long[] id) {
        return R.ok(ksTcService.deleteByid(id));
    }

    /**
     * 查询shp文件信息添加数据并分页
     *
     * @author 窦凯欣
     */
    @ApiOperation(value = "查询shp文件信息添加数据并分页")
    @ApiImplicitParam(value = "xkzh", name = "许可证号", type = "String")
    @GetMapping("/mineLayersController/mountDataAll")
    public R mountDataAll(String xkzh) {
        List<KsTc> dataTable = ksTcService.mountDataAll(xkzh);
        // 将数据拷贝到Vo返回类中，这里如果在业务层进行数据拷贝会导致分页的total数据失效
        List<KsTcVo> ksTcVos = BeanUtil.copyToList(dataTable, KsTcVo.class);
        return R.ok(ksTcVos);
    }

    /**
     * 查询ks全部数据
     *
     * @author 窦凯欣
     */
    @ApiOperation(value = "查询KsTc全部数据(预览数据)")
    @GetMapping("/mineLayersController/getKsDataAll")
    public TableDataInfo<KsTc> getKsDataAll() {
        startPage();
        return getDataTable(ksTcService.getKsDataAll());
    }

    /**
     * 查询行政区二级数据
     *
     * @author 窦凯欣
     */
    @ApiOperation(value = "查询行政区二级数据")
    @GetMapping("/mineLayersController/getXzq")
    public R getXzq() {
        return R.ok(ksJbxxhzxService.getXzq());
    }

    /**
     * 获取矿权类型下拉框
     *
     * @author 窦凯欣
     */
    @ApiOperation(value = "获取矿权类型下拉框")
    @GetMapping("/mineLayersController/getKql")
    public R getKql() {
        return R.ok(ksJbxxhzxService.getKql());
    }

    /**
     * 查询列表正射影像管理列表
     *
     * @param ksJbxxhzRequestBodyParam
     * @author 郭伟明
     */
    @ApiOperation("查询列表正射影像管理列表")
    @GetMapping("/KsJbxxhz/queryKsJbxxhz")
    private R KsJbxxhz(KsJbxxhzRequestBodyParam ksJbxxhzRequestBodyParam) {
        return R.ok(ksJbxxhzxService.selectKsJbxxhzList(ksJbxxhzRequestBodyParam));
    }

    /**
     * 编辑回显
     *
     * @param ksDomParam
     * @param pageable
     * @return
     * @return 郭伟明
     * @author 郭伟明
     */
    @ApiOperation("编辑回显")
    @GetMapping("/KsJbxxhz/EditEcho")
    private R EditEcho(KsDomParam ksDomParam, Pageable pageable) {
        return R.ok(ksJbxxhzxService.EditEcho(ksDomParam, pageable));
    }

    /**
     * 获取下拉框（许可证号、矿山名称）
     *
     * @param ksJbxxhzRequestParam
     * @author 郭梓强
     * @since 2024年5月27日10:46:12
     */
    @ApiOperation("获取下拉框（许可证号、矿山名称）")
    @GetMapping("/ksJbxxhz/selWhole")
    @ApiResponse(code = 200, message = "获取下拉框（许可证号、矿山名称）成功", response = KsJbXxHzDropdownVo.class)
    public R selWhole(KsJbxxhzRequestParam ksJbxxhzRequestParam) {
        return R.ok(ksJbxxhzxService.selWhole(ksJbxxhzRequestParam));
    }

    /**
     * 获取矿山基本信息汇总（条件、分页）
     *
     * @author 郭梓强
     * @since 2024年5月27日20:25:10
     */
    @ApiOperation("获取矿山基本信息汇总（条件、分页）")
    @GetMapping("/ksJbxxhz/selInformation")
    @ApiResponse(code = 200, message = "获取矿山基本信息汇总（条件、分页）", response = KsJbXxHzReturnVo.class)
    public TableDataInfo<List<KsJbXxHzReturnVo>> selInformation(KsJbxxhzRequestParam ksJbxxhzRequestParam) {
        startPage();
        TableDataInfo dataTable = getDataTable(ksJbxxhzxService.selInformation(ksJbxxhzRequestParam));
        dataTable.setRows(ksJbxxhzxService.Transformation(dataTable.getRows()));
        return dataTable;
    }

    /**
     * 新设-矿山基本信息汇总
     *
     * @author 郭梓强
     * @since 2024年5月27日20:43:10
     */
    @ApiOperation("新设-矿山基本信息汇总")
    @PostMapping("/ksJbxxhz/addInformation")
    @ApiResponse(code = 200, message = "新设-矿山基本信息汇总", response = Boolean.class)
    public R<Boolean> addInformation(@Validated @RequestBody KsJbxxhzRequestParam ksJbxxhzRequestParam) throws InvocationTargetException, IllegalAccessException {
        return R.ok(ksJbxxhzxService.addInformation(ksJbxxhzRequestParam));
    }

    /**
     * 删除-矿山基本信息汇总（id）
     *
     * @author 郭梓强
     * @since 2024年5月27日20:45:23
     */
    @ApiOperation("删除-矿山基本信息汇总")
    @GetMapping("/ksJbxxhz/delJgKsJbXxHz")
    @ApiResponse(code = 200, message = "删除-矿山基本信息汇总", response = Boolean.class)
    @ApiImplicitParam(name = "id", value = "矿山基本信息汇总id", type = "Integer")
    public R<Boolean> delJgKsJbXxHz(Integer id) {
        return R.ok(ksJbxxhzxService.delJgKsJbXxHz(id));

    }

    /**
     * 延续-矿山基本信息汇总
     *
     * @author 郭梓强
     * @since 2024年5月27日20:46:10
     */
    @ApiOperation("延续-矿山基本信息汇总")
    @PostMapping("/ksJbxxhz/updJgKsJbXxHz")
    @ApiResponse(code = 200, message = "延续-矿山基本信息汇总", response = Boolean.class)
    public R<Boolean> updJgKsJbXxHz(@Validated @RequestBody KsJbxxhzRequestParam ksJbxxhzRequestParam) throws InvocationTargetException, IllegalAccessException {
        return R.ok(ksJbxxhzxService.updJgKsJbXxHz(ksJbxxhzRequestParam));
    }
}
